// pages/staff_mine/edit_info/index.js

const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        realname: '',
        phone: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        // 验证登录
        app.verifyLogin();

        // 获取数据
        app.execute(function() {
            let page = app.getPage();
            page.getInfo(page);
        });

    },

    /**
     * 自定义函数--获取信息
     */
    getInfo: function (page) {
        wx.showLoading({
            title: '获取信息...',
        });
        wx.request({
            url: app.data.service_url + 'staff_mine/get_info',
            header: {
                'Client-Token': app.data.client_token,
            },
            method: 'GET',
            success: function(response) {
                wx.hideLoading();
                if (200 === response.statusCode) {
                    if (9 === response.data.errno) {
                        wx.redirectTo({
                            url: app.data.staff_uncertified_jump + '?message=' + response.data.message,
                        });
                    } else if (0 === response.data.errno) {
                        page.setData({
                            realname: response.data.data.realname,
                            phone: response.data.data.phone
                        });
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.hideLoading();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    },
    
    /**
     * 自定义函数--改变输入框内容
    */
    changeInput: function(e) {
        this.data[e.target.dataset.name] = e.detail.value;
    },

    /**
     * 自定义函数--提交信息
     */
    submitInfo: function() {

        let data = {
            realname: this.data.realname,
            phone: this.data.phone,
        };

        if (0 === data.realname.length) {
            wx.showToast({
                title: '姓名不能为空',
                icon: 'none'
            });
            return;
        };
        if (0 === data.phone.length) {
            wx.showToast({
                title: '手机号码必须为11位数字',
                icon: 'none'
            });
            return;
        };

        wx.showLoading({
            title: '获取信息...',
        });
        wx.request({
            url: app.data.service_url + 'staff_mine/edit_info',
            header: {
                'Client-Token': app.data.client_token,
            },
            method: 'POST',
            data: data,
            success: function(response) {
                wx.hideLoading();
                if (200 === response.statusCode) {
                    if (9 === response.data.errno) {
                        wx.redirectTo({
                            url: app.data.staff_uncertified_jump + '?message=' + response.data.message,
                        });
                    } else if (0 === response.data.errno) {
                        wx.showToast({
                            title: response.data.message,
                        });
                        setTimeout(function() {
                            wx.navigateBack({
                                delta: 1
                            });
                        }, 1500);
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.hideLoading();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    }
})